/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer.actions;

import java.awt.Font;
import java.awt.GridLayout;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import org.heresylabs.rostercomposer.Composer;
import org.heresylabs.rostercomposer.Main;
import org.heresylabs.rostercomposer.formatter.CompactFormatter;
import org.heresylabs.rostercomposer.formatter.Formatter;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class PreviewCompactAction extends AbstractComposerAction {

    public PreviewCompactAction() {
        putValue(NAME, "Preview");
        putValue(SHORT_DESCRIPTION, "Preview roster in compact format");
        putValue(SMALL_ICON, new ImageIcon(Main.class.getResource("images/document-preview.png")));
    }

    @Override
    protected void performAction(Composer composer) {
        StringWriter out = new StringWriter();
        PrintWriter writer = new PrintWriter(out);

        Formatter formatter = getFormatter(composer);
        formatter.write(writer);
        writer.close();

        JTextArea area = new JTextArea(out.toString(), 20, 50);
        area.setFont(createFont(area.getFont().getSize()));
        area.setEditable(false);

        JScrollPane scroll = new JScrollPane(area);

        JPanel panel = new JPanel(new GridLayout(1, 1));
        panel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
        panel.add(scroll);

        JDialog dialog = new JDialog(SwingUtilities.windowForComponent(composer));
        dialog.setContentPane(panel);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }

    protected Formatter getFormatter(Composer composer) {
        return new CompactFormatter(composer.getRoster());
    }

    private Font createFont(int size) {
        try {
            InputStream in = getClass().getResourceAsStream("/org/heresylabs/rostercomposer/fonts/DejaVuLGCSansMono.ttf");
            Font font = Font.createFont(Font.TRUETYPE_FONT, in);
            in.close();
            return font.deriveFont((float) size);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return new Font(Font.MONOSPACED, Font.PLAIN, size);
    }

}
